package com.dynabook.ems.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dynabook.ems.constant.CommonConstant;
import com.dynabook.ems.mapper.EmsCategoryMapper;
import com.dynabook.ems.model.dto.CategoryListDTO;
import com.dynabook.ems.model.dto.CategoryOptions;
import com.dynabook.ems.model.entity.EmsCategory;
import com.dynabook.ems.model.vo.CategoryMachine;
import com.dynabook.ems.service.EmsCategoryService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author tester
* @description 针对表【ems_category】的数据库操作Service实现
* @createDate 2025-06-24 09:03:44
*/
@Service
public class EmsCategoryServiceImpl extends ServiceImpl<EmsCategoryMapper, EmsCategory>
    implements EmsCategoryService{

    @Override
    public CategoryMachine getCategoryByItemCode(String itemCode) {

        return null;
    }

    @Override
    public Page<EmsCategory> getEmsCategoryList(CategoryListDTO categoryListDTO) {
         String itemCode = categoryListDTO.getItemCode();
         int current = categoryListDTO.getCurrent();
         int pageSize = categoryListDTO.getPageSize();
         Page<EmsCategory> page = new Page<>(current, pageSize);
         
         // 创建查询条件
         QueryWrapper<EmsCategory> queryWrapper = new QueryWrapper<>();
         
         // 如果itemCode不为空，添加模糊查询条件
         if (StrUtil.isNotBlank(itemCode)) {
             queryWrapper.like("item_code", itemCode);
         }
         
         // 添加排序
         String sortField = categoryListDTO.getSortField();
         String sortOrder = categoryListDTO.getSortOrder();
         if (StrUtil.isNotBlank(sortField)) {
             queryWrapper.orderBy(true, 
                     CommonConstant.SORT_ORDER_ASC.equals(sortOrder),
                     sortField);
         } else {
             // 默认按id排序
             queryWrapper.orderByAsc("id");
         }
         
         // 执行分页查询
         Page<EmsCategory> resultPage = this.page(page, queryWrapper);
         
         // 返回查询结果列表
         return resultPage;
    }

    @Override
    public Boolean updateItemCode(EmsCategory emsCategory) {
         Integer id = emsCategory.getId();
         String bigCategory = emsCategory.getBigCategory();
         String category = emsCategory.getCategory();
        UpdateWrapper<EmsCategory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("big_category", bigCategory);
        updateWrapper.set("category", category);
        return this.update(updateWrapper);
    }

    @Override
    public EmsCategory getItemCode(String id) {
        EmsCategory emsCategory = this.baseMapper.selectById(id);
        return emsCategory;
    }

    @Override
    public List<CategoryOptions> getCategory() {
        return this.baseMapper.selectDistinctCategory();
    }

    @Override
    public List<CategoryOptions> getDetailCategory(String categoryName) {
        return this.baseMapper.selectDistinctDetailCategory(categoryName);
    }


}




